package com.jtjrenren.android.taxi.passenger.core.user.forgetpwd;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiResultCommonStr;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;
import com.library.weidget.ClearEditText;
import com.library.utils.RegexUtils;
import com.library.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/1/23.
 */
public class UserForgetPwdActivity extends BaseActivity {
    @BindView(R.id.user_forgetpwd_frist)
    LinearLayout ll_frist;
    @BindView(R.id.user_forgetpwd_phone)
    ClearEditText et_phone;
    @BindView(R.id.user_forgetpwd_code)
    ClearEditText et_code;
    @BindView(R.id.user_forgetpwd_pwd)
    ClearEditText et_pwd;
    @BindView(R.id.user_forgetpwd_getcode)
    TextView tv_getcode;
    @BindView(R.id.title)
    TextView tv_title;

    CountDownTimer countDownTimer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tv_getcode.setText((int) millisUntilFinished / 1000 + getString(R.string.afterrsend));
        }

        @Override
        public void onFinish() {
            tv_getcode.setEnabled(true);
            tv_getcode.setText(getString(R.string.getCode));
        }
    };

    private int step = 1;

    private String phone;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null)
            countDownTimer.cancel();
        countDownTimer = null;
    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_user_forgetpwd;
    }

    @Override
    public void init() {
        tv_title.setText("忘记密码");
    }

    @OnClick(R.id.title_left)
    public void clickBack() {
        finish();
    }


    @OnClick(R.id.user_forgetpwd_getcode)
    public void getYzm() {
        phone = et_phone.getText().toString();
        if (RegexUtils.isPhoneNo(phone)) {
            ApiWorks.checkPhoneUnie(phone, new ApiWorks.ResponseListener<ApiResultCommonStr>() {
                @Override
                public void onResponse(ApiResultCommonStr apiResultCommonStr) {
                    if (apiResultCommonStr != null && apiResultCommonStr.getCode() == 0) {
                        countDownTimer.start();
                        tv_getcode.setEnabled(false);
                        ApiWorks.sendAuthCode(phone, Constants.DEFAULT_SMSTYPE_REGISTER, null);
                    }else {
                        displayShort("手机号未注册");
                    }
                }
            });
        } else {
            displayShort("请输入正确的手机号码");
        }
    }

    @OnClick(R.id.user_forgetpwd_commit)
    public void commit() {
        if (step == 1) {
            String code = et_code.getText().toString().trim();

            if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
                displayShort("请输入正确信息");
                return;
            }

            ApiWorks.authPhoneByCode(code, phone, Constants.DEFAULT_SMSTYPE_REGISTER, new ApiWorks.ResponseListener<ApiResultCommonStr>() {
                @Override
                public void onResponse(ApiResultCommonStr apiResultCommonStr) {
                    if (apiResultCommonStr != null && apiResultCommonStr.getCode() == 1) {
                        ll_frist.setVisibility(View.GONE);
                        et_pwd.setVisibility(View.VISIBLE);
                        step = 2;
                    } else
                        Toast.makeText(UserForgetPwdActivity.this, "验证码校验失败！", Toast.LENGTH_SHORT).show();
                }
            });
        } else if (step == 2) {
            String pwd = et_pwd.getText().toString().trim();

            if (StringUtils.isEmpty(pwd)) {
                displayShort(getString(R.string.mimabunengweikong));
                return;
            }

            if (pwd.length() < 6 || pwd.length() > 20) {
                displayShort(getString(R.string.mimaxianzhi));
                return;
            }

            ApiWorks.ResetPassword(phone, ModuleUtils.DesEncry(pwd), new ApiWorks.ResponseListener<ApiResultCommonStr>() {
                @Override
                public void onResponse(ApiResultCommonStr apiResultCommonStr) {
                    if (apiResultCommonStr != null && apiResultCommonStr.getCode() == 1) {
                        displayShort(getString(R.string.gongxininxiugaichenggong));
                        finish();
                    } else
                        displayShort("密码重置失败！");
                }
            });
        }
    }
}
